package cn.com.zzl.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;

import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * Description：JSON工具类（com.alibaba.fastjson）
 * Created by 朱正磊 on 2017-05-24 10:13.
 */
public class JSONUtils {

	/**
	 * @Desc 生成JSONObject字符串(有且只有一个键值对)
	 * @param key
	 * @param value
	 * @return
	 */
	public static String generateJsonObjectString(String key, Object value){
		if(StringUtils.isNotBlank(key)){
			JSONObject jsonObject = new JSONObject();
			jsonObject.put(key,value);
			return jsonObject.toString();
		}
		return "";
	}

	/**
	 * @Desc Map转JSONObject字符串
	 * @param map
	 * @return
	 */
	public static String map2JsonObjectString(Map<String,Object> map){
		if(map != null){
			JSONObject jsonObject = new JSONObject();

			//方法一：该方法比较好
			/*for (Map.Entry<String, Object> entry : map.entrySet()) {
				jsonObject.put(entry.getKey(), entry.getValue());
			}*/

			//方法二：
			Iterator<Map.Entry<String,Object>> iterator = map.entrySet().iterator();
			while(iterator.hasNext()){
				Map.Entry<String,Object> entry = iterator.next();
				jsonObject.put(entry.getKey(),entry.getValue());
			}
			return jsonObject.toString();
		}
		return "";
	}

	/**
	 * @Desc mapList转JSONArray字符串
	 * @param mapList
	 * @return
	 */
	public static String mapList2JsonArrayString(List<Map<String,Object>> mapList){
		if(mapList != null){
			return convertMapList2JsonArray(mapList,new JSONArray()).toString();
		}
		return "";
	}

	/**
	 * 说明：返回的Map集合是<String, Object>类型的
	 * @Desc JSONObject字符串转Map
	 * @param jsonObjectString
	 * @return
	 */
	public static Map<String, Object> jsonObjectString2Map(String jsonObjectString){
		return JSON.parseObject(jsonObjectString, new TypeReference<Map<String, Object>>(){});
	}
	
	/**
	 * 说明：返回的Map集合是<String, String>类型的
	 * @Desc JSONObject字符串转Map
	 * @param jsonObjectString
	 * @return
	 */
	public static Map<String, String> jsonObjectString2MapStr(String jsonObjectString){
		return JSON.parseObject(jsonObjectString, new TypeReference<Map<String, String>>(){});
	}

	/**
	 * @Desc JSONArray字符串转mapList
	 * @param jsonArrayString
	 * @return
	 */
	public static List<Map<String, Object>> jsonArrayString2MapList(String jsonArrayString){
		return JSON.parseObject(jsonArrayString, new TypeReference<List<Map<String, Object>>>(){});
	}

	/**
	 * @Desc Map转JSONObject
	 * @param map
	 * @return
	 */
	public static JSONObject map2JsonObject(Map<String,Object> map){
		if(map != null){
			JSONObject jsonObject = new JSONObject();
			Iterator<Map.Entry<String,Object>> iterator = map.entrySet().iterator();
			while(iterator.hasNext()){
				Map.Entry<String,Object> entry = iterator.next();
				jsonObject.put(entry.getKey(),entry.getValue());
			}
			return jsonObject;
		}
		return null;
	}

	/**
	 * @Desc mapList转JSONArray
	 * @param mapList
	 * @return
	 */
	public static JSONArray mapList2JsonArray(List<Map<String,Object>> mapList){
		if(mapList != null){
			return convertMapList2JsonArray(mapList,new JSONArray());
		}
		return null;
	}

	/**
	 * @Desc JSONObject转Map
	 * @return
	 */
	public static Map<String, Object> jsonObject2Map(JSONObject jsonObject){
		if(jsonObject != null){
			Map<String, Object> map = new HashMap<String, Object>();

			//方法一：该方法比较好
			/*for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
				map.put(entry.getKey(), entry.getValue());
			}*/

			//方法二：
			Iterator<Map.Entry<String,Object>> iterator = jsonObject.entrySet().iterator();
			while(iterator.hasNext()){
				Map.Entry<String,Object> entry = iterator.next();
				map.put(entry.getKey(),entry.getValue());
			}
			return map;
		}
		return null;
	}

	/**
	 * @Desc JSONArray转mapList
	 * @return
	 */
	public static List<Map<String, Object>> jsonArray2MapList(JSONArray jsonArray){
		if(jsonArray != null){
			List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
			for(Object object : jsonArray){
				JSONObject jsonObject = (JSONObject) object;
				Map<String, Object> map = new HashMap<String, Object>();
				Iterator<Map.Entry<String,Object>> iterator = jsonObject.entrySet().iterator();
				while(iterator.hasNext()){
					Map.Entry<String,Object> entry = iterator.next();
					map.put(entry.getKey(),entry.getValue());
				}
				mapList.add(map);
			}
			return mapList;
		}
		return null;
	}

	/**
	 * @Desc JSONObject字符串转JSONObject
	 * @param JsonObject
	 * @return
	 */
	public static JSONObject jsonObjectString2JsonObject(String JsonObject){
		JSONObject result = null;
		if(StringUtils.isBlank(JsonObject)){
			return result;
		}
		try {
			return JSON.parseObject(JsonObject);
		} catch (Exception e) {
			return result;
		}
	}

	/**
	 * @Desc JSONArray字符串转JSONArray
	 * @param JsonArray
	 * @return
	 */
	public static JSONArray jsonArrayString2JsonArray(String JsonArray){
		JSONArray result = null;
		if(StringUtils.isBlank(JsonArray)){
			return result;
		}
		try {
			return JSON.parseArray(JsonArray);
		} catch (Exception e) {
			return result;
		}
	}

	//**************************************************************************************//

	/**
	 * @Desc JSONObject字符串转javaBean对象
	 * @param jsonObjectString
	 * @param clazz
	 * @param <T>
	 * @return
	 */
	public static <T> T jsonObjectString2JavaBean(String jsonObjectString, Class<T> clazz){
		return JSON.parseObject(jsonObjectString,clazz);
	}

	/**
	 * @Desc JSONArray字符串转javaBean的List集合
	 * @param jsonArrayString
	 * @param clazz
	 * @param <T>
	 * @return
	 */
	public static <T> List<T> jsonArrayString2JavaBeanList(String jsonArrayString, Class<T> clazz){
		return JSON.parseArray(jsonArrayString,clazz);
	}

	/**
	 * @Desc javaBean对象转JSONObject字符串
	 * @param javaBean
	 * @param isFormat 是否进行格式化：true进行格式化，false不进行格式化，null不进行格式化同false
	 * @return
	 */
	public static String javaBean2JsonObjectString(Object javaBean, Boolean isFormat){
		if(isFormat != null){
			return JSON.toJSONString(javaBean,isFormat);
		}
		return JSON.toJSONString(javaBean);
	}

	/**
	 * @Desc javaBean的List集合转JSONArray字符串
	 * @param javaBeanList
	 * @param isFormat 是否进行格式化：true进行格式化，false不进行格式化，null不进行格式化同false
	 * @param <T>
	 * @return
	 */
	public static <T> String javaBeanList2JsonArrayString(List<T> javaBeanList, Boolean isFormat){
		if(isFormat != null){
			return JSON.toJSONString(javaBeanList,isFormat);
		}
		return JSON.toJSONString(javaBeanList);
	}

	/**
	 * @Desc javaBean转JSONObject，其实相当于已经转为了JSONObject字符串（toString）
	 * @param javaBean
	 * @return
	 */
	public static JSONObject javaBean2JsonObject(Object javaBean){
		return (JSONObject) JSON.toJSON(javaBean);
	}

	/**
	 * @Desc javaBean的List转JSONArray，其实相当于已经转为了JSONArray字符串（toString）
	 * @param javaBeanList
	 * @param <T>
	 * @return
	 */
	public static <T> JSONArray javaBeanList2JsonArray(List<T> javaBeanList){
		return (JSONArray) JSON.toJSON(javaBeanList);
	}

	//**************************************************************************************//

	/**
	 * @Desc JSON字符串转JSON，可能是JSONObject，也可能是JSONArray
	 * @param jsonString
	 * @return
	 */
	public static Object jsonString2Json(String jsonString){
		return JSON.parse(jsonString);
	}

	/**
	 * @Desc map转JSONString
	 * @param mapOrMapList
	 * @return 如果参数是Map，那么返回值就是JSONObject字符串；如果参数是List<Map>，那么返回值就是JSONArray字符串。
	 */
	public static String map2JsonString(Object mapOrMapList){
		return JSON.toJSONString(mapOrMapList);
	}

	/**
	 * @Desc MapList 转 JsonArray
	 * @param mapList
	 * @param jsonArray
	 * @return
	 */
	private static JSONArray convertMapList2JsonArray(List<Map<String,Object>> mapList, JSONArray jsonArray){
		for(Map<String,Object> map : mapList){
			JSONObject jsonObject = new JSONObject();
			Iterator<Map.Entry<String,Object>> iterator = map.entrySet().iterator();
			while(iterator.hasNext()){
				Map.Entry<String,Object> entry = iterator.next();
				jsonObject.put(entry.getKey(),entry.getValue());
			}
			jsonArray.add(jsonObject);
		}
		return jsonArray;
	}

	public static void main(String[] args) {

		//System.out.println(generateJsonObjectString("name","zzl"));//{"name":"zzl"}

		//***************************************************************************//

		/*
		String JSONObjectString1 = generateJsonObjectString("name","zzl");
		String JSONObjectString2 = generateJsonObjectString("age",23);
		String JSONObjectString3 = generateJsonObjectString("birthday",new Date());

		JSONArray jsonArray = new JSONArray();

		JSONObject jsonObject1 = jsonObjectString2JsonObject(JSONObjectString1);
		JSONObject jsonObject2 = jsonObjectString2JsonObject(JSONObjectString2);
		JSONObject jsonObject3 = jsonObjectString2JsonObject(JSONObjectString3);

		jsonArray.add(jsonObject1);
		jsonArray.add(jsonObject2);
		jsonArray.add(jsonObject3);

		System.out.println(jsonArray);//[{"name":"zzl"},{"age":23},{"birthday":1495608970900}]
		*/

		//***************************************************************************//

		/*Map<String,Object> map = new HashMap<String,Object>();
		map.put("name","朱正磊");
		map.put("age",27);
		map.put("birthday",CalendarUtil.getNowDate("yyyy-MM-dd"));

		String jsonObjectString = map2JsonObjectString(map);
		System.out.println(jsonObjectString);//{"birthday":"2017-05-24","name":"朱正磊","age":27}

		JSONObject jsonObject = jsonObjectString2JsonObject(jsonObjectString);
		System.out.println(jsonObject);//{"birthday":"2017-05-24","name":"朱正磊","age":27}*/

		//***************************************************************************//

        /*
		Map<String,Object> map1 = new HashMap<String,Object>();
		map1.put("name","朱正磊");
		map1.put("sex","男");
		map1.put("birthday",CalendarUtil.getNowDate("yyyy-MM-dd"));

		Map<String,Object> map2 = new HashMap<String,Object>();
		map2.put("name","翟甜");
		map2.put("sex","女");
		map2.put("birthday",CalendarUtil.getNowDate("yyyy-MM-dd"));

		Map<String,Object> map3 = new HashMap<String,Object>();
		map3.put("name","周润发");
		map3.put("sex","男");
		map3.put("birthday",CalendarUtil.getNowDate("yyyy-MM-dd"));

		List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
		list.add(map1);
		list.add(map2);
		list.add(map3);

		String jsonArrayString = mapList2JsonArrayString(list);
		System.out.println(jsonArrayString);//[{"birthday":"2017-05-24","sex":"男","name":"朱正磊"},{"birthday":"2017-05-24","sex":"女","name":"翟甜"},{"birthday":"2017-05-24","sex":"男","name":"周润发"}]
		JSONArray jsonArray = jsonArrayString2JsonArray(jsonArrayString);
		System.out.println(jsonArray);//[{"birthday":"2017-05-24","sex":"男","name":"朱正磊"},{"birthday":"2017-05-24","sex":"女","name":"翟甜"},{"birthday":"2017-05-24","sex":"男","name":"周润发"}]
        */

		//***************************************************************************//

		/*
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("name","朱正磊");
		map.put("age",27);
		map.put("birthday",CalendarUtil.getNowDate("yyyy-MM-dd"));

		JSONObject jsonObject = map2JsonObject(map);
		System.out.println(jsonObject);//{"birthday":"2017-05-24","name":"朱正磊","age":27}
		Map<String,Object> myMap = jsonObject2Map(jsonObject);
		System.out.println(myMap);//{birthday=2017-05-24, name=朱正磊, age=27}
		*/

		//***************************************************************************//

		/*
		Map<String,Object> map1 = new HashMap<String,Object>();
		map1.put("name","朱正磊");
		map1.put("sex","男");
		map1.put("birthday",CalendarUtil.getNowDate("yyyy-MM-dd"));

		Map<String,Object> map2 = new HashMap<String,Object>();
		map2.put("name","翟甜");
		map2.put("sex","女");
		map2.put("birthday",CalendarUtil.getNowDate("yyyy-MM-dd"));

		Map<String,Object> map3 = new HashMap<String,Object>();
		map3.put("name","周润发");
		map3.put("sex","男");
		map3.put("birthday",CalendarUtil.getNowDate("yyyy-MM-dd"));

		List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
		list.add(map1);
		list.add(map2);
		list.add(map3);

		JSONArray jsonArray = mapList2JsonArray(list);
		System.out.println(jsonArray);//[{"birthday":"2017-05-24","sex":"男","name":"朱正磊"},{"birthday":"2017-05-24","sex":"女","name":"翟甜"},{"birthday":"2017-05-24","sex":"男","name":"周润发"}]
		List<Map<String,Object>> mapList = jsonArray2MapList(jsonArray);
		System.out.println(mapList);//[{birthday=2017-05-24, sex=男, name=朱正磊}, {birthday=2017-05-24, sex=女, name=翟甜}, {birthday=2017-05-24, sex=男, name=周润发}]
		*/

		//***************************************************************************//

		/*
		String jsonObjectString = "{'birthday':'2017-05-24','name':'朱正磊','age':27}";
		//{birthday=2017-05-24, name=朱正磊, age=27}
		Map<String, Object> map = jsonObjectString2Map(jsonObjectString);
		System.out.println(map);

		String jsonArrayString = "[{'birthday':'2017-05-24','sex':'男','name':'朱正磊'},{'birthday':'2017-05-24','sex':'女','name':'翟甜'},{'birthday':'2017-05-24','sex':'男','name':'周润发'}]";
		//[{birthday=2017-05-24, sex=男, name=朱正磊}, {birthday=2017-05-24, sex=女, name=翟甜}, {birthday=2017-05-24, sex=男, name=周润发}]
		List<Map<String, Object>> mapList = jsonArrayString2MapList(jsonArrayString);
		System.out.println(mapList);
		*/

		//***************************************************************************//

		/*
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("username","朱正磊");
		map.put("id",1);
		map.put("birthday",new Date());
		map.put("sex","男");
		map.put("address","苏州");

		String jsonObjectString = map2JsonObjectString(map);
		User user = jsonObjectString2JavaBean(jsonObjectString,User.class);
		System.out.println(user);//{id=1, username=朱正磊, birthday=Thu May 25 14:52:25 CST 2017, sex=男, address=苏州}
		*/

		//***************************************************************************//

		/*
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("username","朱正磊");
		//map.put("id",1);
		map.put("birthday",new Date());
		//map.put("sex","男");
		map.put("address","苏州");

		String jsonObjectString = map2JsonObjectString(map);
		User user = jsonObjectString2JavaBean(jsonObjectString,User.class);
		System.out.println(user);//{id=null, username=朱正磊, birthday=Thu May 25 15:39:41 CST 2017, sex=null, address=苏州}
		*/

		//***************************************************************************//

		/*
		Map<String,Object> map1 = new HashMap<String,Object>();

		map1.put("username","朱正磊");
		map1.put("id",1);
		map1.put("birthday",new Date());
		map1.put("sex","男");
		map1.put("address","苏州");

		Map<String,Object> map2 = new HashMap<String,Object>();
		map2.put("username","马云");
		map2.put("id",2);
		map2.put("birthday",new Date());
		map2.put("sex","男");
		map2.put("address","杭州");

		Map<String,Object> map3 = new HashMap<String,Object>();
		map3.put("username","王思聪");
		map3.put("id",3);
		map3.put("birthday",new Date());
		map3.put("sex","男");
		map3.put("address","北京");

		List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
		list.add(map1);
		list.add(map2);
		list.add(map3);

		String jsonArrayString = mapList2JsonArrayString(list);
		List<User> userList = jsonArrayString2JavaBeanList(jsonArrayString,User.class);
		//[{id=1, username=朱正磊, birthday=Thu May 25 15:25:01 CST 2017, sex=男, address=苏州}, {id=2, username=马云, birthday=Thu May 25 15:25:01 CST 2017, sex=男, address=杭州}, {id=3, username=王思聪, birthday=Thu May 25 15:25:01 CST 2017, sex=男, address=北京}]
		System.out.println(userList);
		*/

		//***************************************************************************//

		/*
		User user = new User();
		user.setId(1);
		user.setUsername("朱正磊");
		user.setSex("男");
		user.setBirthday(new Date());
		user.setAddress("苏州");

		//{"address":"苏州","birthday":1495699487537,"id":1,"sex":"男","username":"朱正磊"}
		System.out.println(javaBean2JsonObjectString(user, null));

		//{"address":"苏州","birthday":1495699487537,"id":1,"sex":"男","username":"朱正磊"}
		System.out.println(javaBean2JsonObjectString(user, false));

//		{
//			"address":"苏州",
//			"birthday":1495699487537,
//			"id":1,
//			"sex":"男",
//			"username":"朱正磊"
//		}
		System.out.println(javaBean2JsonObjectString(user, true));
		*/

		//***************************************************************************//

		/*
		User user1 = new User();
		user1.setId(1);
		user1.setUsername("朱正磊");
		user1.setSex("男");
		user1.setBirthday(new Date());
		user1.setAddress("苏州");

		User user2 = new User();
		user2.setId(2);
		user2.setUsername("马云");
		user2.setSex("男");
		user2.setBirthday(new Date());
		user2.setAddress("杭州");

		User user3 = new User();
		user3.setId(3);
		user3.setUsername("马化腾");
		user3.setSex("男");
		user3.setBirthday(new Date());
		user3.setAddress("深圳");

		List<User> userList = new ArrayList<User>();
		userList.add(user1);
		userList.add(user2);
		userList.add(user3);

		//[{"address":"苏州","birthday":1495700051789,"id":1,"sex":"男","username":"朱正磊"},{"address":"杭州","birthday":1495700051789,"id":2,"sex":"男","username":"马云"},{"address":"深圳","birthday":1495700051789,"id":3,"sex":"男","username":"马化腾"}]
		System.out.println(javaBeanList2JsonArrayString(userList,null));

		//[{"address":"苏州","birthday":1495700051789,"id":1,"sex":"男","username":"朱正磊"},{"address":"杭州","birthday":1495700051789,"id":2,"sex":"男","username":"马云"},{"address":"深圳","birthday":1495700051789,"id":3,"sex":"男","username":"马化腾"}]
		System.out.println(javaBeanList2JsonArrayString(userList,false));

//		[
//			{
//				"address":"苏州",
//					"birthday":1495700051789,
//					"id":1,
//					"sex":"男",
//					"username":"朱正磊"
//			},
//			{
//				"address":"杭州",
//					"birthday":1495700051789,
//					"id":2,
//					"sex":"男",
//					"username":"马云"
//			},
//			{
//				"address":"深圳",
//					"birthday":1495700051789,
//					"id":3,
//					"sex":"男",
//					"username":"马化腾"
//			}
//		]
		System.out.println(javaBeanList2JsonArrayString(userList,true));
		*/

		//***************************************************************************//

		/*
		User user = new User();
		user.setId(1);
		user.setUsername("朱正磊");
		user.setSex("男");
		user.setBirthday(new Date());
		user.setAddress("苏州");

		//{"birthday":1495700595892,"address":"苏州","id":1,"sex":"男","username":"朱正磊"}
		System.out.println(javaBean2JsonObject(user));
		*/

		//***************************************************************************//

		/*
		User user1 = new User();
		user1.setId(1);
		user1.setUsername("朱正磊");
		user1.setSex("男");
		user1.setBirthday(new Date());
		user1.setAddress("苏州");

		User user2 = new User();
		user2.setId(2);
		user2.setUsername("马云");
		user2.setSex("男");
		user2.setBirthday(new Date());
		user2.setAddress("杭州");

		User user3 = new User();
		user3.setId(3);
		user3.setUsername("马化腾");
		user3.setSex("男");
		user3.setBirthday(new Date());
		user3.setAddress("深圳");

		List<User> userList = new ArrayList<User>();
		userList.add(user1);
		userList.add(user2);
		userList.add(user3);

		//[{"birthday":1495700780261,"address":"苏州","id":1,"sex":"男","username":"朱正磊"},{"birthday":1495700780261,"address":"杭州","id":2,"sex":"男","username":"马云"},{"birthday":1495700780261,"address":"深圳","id":3,"sex":"男","username":"马化腾"}]
		System.out.println(javaBeanList2JsonArray(userList));
		*/

		//***************************************************************************//

		/*
		String jsonObjectString = "{'birthday':'2017-05-24','name':'朱正磊','age':27}";
		JSONObject jsonObject = (JSONObject) jsonString2Json(jsonObjectString);
		//{"birthday":"2017-05-24","name":"朱正磊","age":27}
		System.out.println(jsonObject);

		String jsonArrayString = "[{'birthday':'2017-05-24','sex':'男','name':'朱正磊'},{'birthday':'2017-05-24','sex':'女','name':'翟甜'},{'birthday':'2017-05-24','sex':'男','name':'周润发'}]";
		JSONArray jsonArray = (JSONArray) jsonString2Json(jsonArrayString);
		//[{"birthday":"2017-05-24","sex":"男","name":"朱正磊"},{"birthday":"2017-05-24","sex":"女","name":"翟甜"},{"birthday":"2017-05-24","sex":"男","name":"周润发"}]
		System.out.println(jsonArray);
		*/

		//***************************************************************************//

		/*
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("username","朱正磊");
		map.put("id",1);
		map.put("birthday",new Date());
		map.put("sex","男");
		map.put("address","苏州");

		String jsonObjectString = map2JsonString(map);
		//{"birthday":1495702672837,"address":"苏州","sex":"男","id":1,"username":"朱正磊"}
		System.out.println(jsonObjectString);
		*/

		//***************************************************************************//

		/*
		Map<String,Object> map1 = new HashMap<String,Object>();
		map1.put("username","朱正磊");
		map1.put("id",1);
		map1.put("birthday",new Date());
		map1.put("sex","男");
		map1.put("address","苏州");

		Map<String,Object> map2 = new HashMap<String,Object>();
		map2.put("username","马云");
		map2.put("id",2);
		map2.put("birthday",new Date());
		map2.put("sex","男");
		map2.put("address","杭州");

		Map<String,Object> map3 = new HashMap<String,Object>();
		map3.put("username","王思聪");
		map3.put("id",3);
		map3.put("birthday",new Date());
		map3.put("sex","男");
		map3.put("address","北京");

		List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
		list.add(map1);
		list.add(map2);
		list.add(map3);

		String jsonArrayString = map2JsonString(list);
		//[{"birthday":1495702865373,"address":"苏州","sex":"男","id":1,"username":"朱正磊"},{"birthday":1495702865373,"address":"杭州","sex":"男","id":2,"username":"马云"},{"birthday":1495702865373,"address":"北京","sex":"男","id":3,"username":"王思聪"}]
		System.out.println(jsonArrayString);
		*/

		//***************************************************************************//


	}

}
